#include "ShaderUtil.h"

//-------------------------------------------------------------------
ShaderUtil::ShaderUtil()
{ }

//-------------------------------------------------------------------
ShaderUtil::~ShaderUtil()
{ }

//-------------------------------------------------------------------
void ShaderUtil::CompileEffect(
	ID3D11Device*	device,			// Pointer to application's device.
	std::wstring&	filename,		// Reference of the filename string for the HLSL file to compile.
	ID3DX11Effect** effect)			// Pointer-pointer to the compiled effect interface.
{
	DWORD shaderFlags = 0;
#if defined( DEBUG ) || defined( _DEBUG )
	shaderFlags |= D3D10_SHADER_DEBUG;
	shaderFlags |= D3D10_SHADER_SKIP_OPTIMIZATION;
#endif

	ID3D10Blob* compileShader   = 0;
	ID3D10Blob* compilationMsgs = 0;
	HRESULT hr;

	// Compile shader.
	hr = D3DX11CompileFromFile(filename.c_str(), 0, 0, 0, "fx_5_0", shaderFlags,
		0, 0, &compileShader, &compilationMsgs, 0);

	// Error checking.
	if (compilationMsgs != 0)
	{
		MessageBoxA(0, (char*)compilationMsgs->GetBufferPointer(), 0, 0);
		compilationMsgs->Release();
		compilationMsgs = nullptr;
	}

	if (FAILED(hr))
	{
		DXTrace(__FILE__, (DWORD)__LINE__, hr, L"D3DX11CompileFromFile", true);
	}

	// Create ID3DX11Effect.
	hr = D3DX11CreateEffectFromMemory(compileShader->GetBufferPointer(), compileShader->GetBufferSize(),
		0, device, effect);

	if (FAILED(hr))
	{
		DXTrace(__FILE__, (DWORD)__LINE__, hr, L"D3DX11CompileFromFile", true);
	}

	compileShader->Release();
	compileShader = nullptr;
}